using UnityEngine;
using ZCUtil.Touch;
using ZCUtil.Factory;

namespace ZCTouchDrawBoard
{
    public class ColorPaint : BasePaint
    {
        #region ConstField
        private const string SHADER_NAME = "Hidden/ColorPenShader";
        private const int PEN_TEXTURE_INDEX = 0;
        #endregion

        #region PublicMethod
        public override PaintAttribute PaintAttribute
        {
            get
            {
                return m_PaintModel;
            }
            set
            {
                if (value == null)
                {
                    return;
                }
                else
                {
                    if (value.MaterialPath != null && !value.MaterialPath.Equals(m_PaintModel.MaterialPath))
                    {
                        m_Channels[PEN_TEXTURE_INDEX] = Resources.Load<Texture2D>(value.MaterialPath);
                    }
                    m_PaintModel.Copy(value);
                }
            }
        }
        #endregion

        #region PublicMethod
        public ColorPaint() : base(SHADER_NAME)
        {

        }

        public ColorPaint(PaintAttribute paintModel) : base(paintModel, SHADER_NAME)
        {

        }
        #endregion

        #region PublicMethod
        public override void DrawToPaper(Vector3[] positions, BasePaperView paper, Mesh mesh = null)
        {
            DrawToPaper(positions, paper, paper.OrthoMatrix, mesh);
        }

        public void DrawToPaper(Vector3[] positions, BasePaperView paper, Matrix4x4 orthoMatrix, Mesh mesh = null)
        {
            if (positions == null)
            {
                Logger.w($"{this} position is null.");
                return;
            }
            // 先绘制到B
            paper.Use();
            // 设置ShaderPass
            m_Shader.SetTexture("_PenMaskTex", m_Channels[PEN_TEXTURE_INDEX]);
            m_Shader.SetColor("_Color", PaintAttribute.PenColor);
            m_Shader.SetMatrix("_ProjectMatrix", orthoMatrix);
            m_Shader.SetPass(0);
            // 模板Mesh
            Mesh templateMesh = mesh;
            Vector3[] templateVertices = new Vector3[4];
            for (int j = 0; j < 4; j++)
            {
                templateVertices[j] = templateMesh.vertices[j] * PaintAttribute.PenSize / 2.0f;
            }
            // 构造mesh
            Mesh positionMeshs = new Mesh();
            Vector3[] vertices = new Vector3[positions.Length * 4];
            Vector2[] uv = new Vector2[positions.Length * 4];
            int[] triangles = new int[positions.Length * 6];
            for (int i = 0; i < positions.Length; i++)
            {
                if (!CanDrawing(positions[i]))
                    continue;
                for (int j = 0; j < 4; j++)
                {
                    vertices[i * 4 + j] = positions[i] + templateVertices[j];
                    uv[i * 4 + j] = templateMesh.uv[j];
                }
                for (int j = 0; j < 6; j++)
                {
                    triangles[i * 6 + j] = templateMesh.triangles[j] + i * 4;
                }
            }
            positionMeshs.vertices = vertices;
            positionMeshs.uv = uv;
            positionMeshs.triangles = triangles;
            Graphics.DrawMeshNow(positionMeshs, Matrix4x4.identity);
            // 显示
            paper.Display();
        }
        #endregion
    }
}
